package com.intelligent.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.intelligent.utils.Result;
import com.intelligent.dto.WarehouseReceiptQueryDTO;
import com.intelligent.pojo.WarehouseReceipt;
import com.intelligent.service.WarehouseReceiptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 入库单控制器
 */
@RestController
@RequestMapping("/warehouse/receipt")
public class WarehouseReceiptController {

    @Autowired
    private WarehouseReceiptService warehouseReceiptService;

    //分页查询
    @PostMapping("/page")
    public Result<IPage<WarehouseReceipt>> pageList(@RequestBody WarehouseReceiptQueryDTO query) {
        IPage<WarehouseReceipt> page = warehouseReceiptService.pageList(query);
        return Result.success(page);
    }

    //入库单详情
    @GetMapping("/getDetail")
    public Result<WarehouseReceipt> getDetail(@RequestParam("id") Long id) {
        WarehouseReceipt receipt = warehouseReceiptService.getDetailById(id);
        if (receipt == null) {
            return Result.error("入库单不存在");
        }
        return Result.success(receipt);
    }

    //新增
    @PostMapping
    public Result<Boolean> add(@RequestBody WarehouseReceipt receipt) {
        try {
            boolean success = warehouseReceiptService.add(receipt);
            return success ? Result.success(true) : Result.error("新增入库单失败");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}
